//
//  LLAttributedLabel.m
//  LLAttributedStringTest
//
//  Created by ZPP on 13-2-19.
//  Copyright (c) 2013年 ZPP. All rights reserved.
//

#import "LLAttributedLabel.h"

@interface LLAttributedLabel(){

}
@end

@implementation LLAttributedLabel
@synthesize attString = _attString;

- (void)dealloc{
    self.attString = nil;
    self.defaultFont = nil;
    self.defaultColor = nil;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.defaultColor = [UIColor blackColor];
        self.defaultFont = [UIFont systemFontOfSize:15];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    if (_attString == nil)
    {
        return;
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGMutablePathRef path = CGPathCreateMutable(); //1
    CGPathAddRect(path, NULL, self.bounds );
    
    
    CTFramesetterRef framesetter =
    CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_attString); //3
    CTFrameRef frame =
    CTFramesetterCreateFrame(framesetter,
                             CFRangeMake(0, [_attString length]), path, NULL);
    
    CTFrameDraw(frame, context); //4
    
    CFRelease(frame); //5
    CFRelease(path);
    CFRelease(framesetter);
}

- (void)drawRect11:(CGRect)rect{
//    [super drawRect:rect];//默认lable的 描述方法
    if (self.layer.sublayers&&self.layer.sublayers.count) {
        
        
        for(CALayer *ly in self.layer.sublayers){
            if([ly isKindOfClass: [CATextLayer class]])
            {
                [ly removeFromSuperlayer];
                break;
            }
        }
    }
    
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.contentsScale = 2.0;
    textLayer.string = _attString;
    textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self.layer addSublayer:textLayer];
}

- (void)setText:(NSString *)text{
    [super setText:text];
    
    if (text == nil) {
        self.attString = nil;
    }else{
        self.attString = [[NSMutableAttributedString alloc] initWithString:text];
        
        //段落  
        //line break  
        CTParagraphStyleSetting lineBreakMode;  
        CTLineBreakMode lineBreak = kCTLineBreakByWordWrapping; //换行模式  
        lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;  
        lineBreakMode.value = &lineBreak;  
        lineBreakMode.valueSize = sizeof(CTLineBreakMode);  
        //行间距  
        CTParagraphStyleSetting LineSpacing;  
        CGFloat spacing = 8.0;  //指定间距  
        LineSpacing.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;  
        LineSpacing.value = &spacing;  
        LineSpacing.valueSize = sizeof(CGFloat);  
        
        CTParagraphStyleSetting settings[] = {lineBreakMode,LineSpacing};  
        CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 2);   //第二个参数为settings的长度  
        [self.attString addAttribute:(NSString *)kCTParagraphStyleAttributeName  
                               value:(id)CFBridgingRelease(paragraphStyle)  
                               range:NSMakeRange(0, self.attString.length)];  
    }
}

// 设置某段字的颜色
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTForegroundColorAttributeName
                        value:(id)color.CGColor
                        range:NSMakeRange(location, length)];
}

// 设置某段字的字体
- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTFontAttributeName
                        value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
                                                       font.pointSize,
                                                       NULL))
                        range:NSMakeRange(location, length)];
}

// 设置某段字的风格
- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                        value:(id)[NSNumber numberWithInt:style]
                        range:NSMakeRange(location, length)];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)addTitle:(NSString*)title withFont:(UIFont*)font withColor:(UIColor *)color withStyle:(CTUnderlineStyle)style
{
    
    if (self.attString == nil)
    {
        self.attString = [[NSMutableAttributedString alloc] initWithString:@""];
    }
    
    NSMutableAttributedString *appendingAttString = [[NSMutableAttributedString alloc] initWithString:title];
    
    [appendingAttString addAttribute:(NSString *)kCTForegroundColorAttributeName
                               value:(id)color.CGColor
                               range:NSMakeRange(0, appendingAttString.length)];
    
    
    [appendingAttString addAttribute:(NSString *)kCTFontAttributeName
                       value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
                                                      font.pointSize,
                                                      NULL))
                       range:NSMakeRange(0, appendingAttString.length)];
    
    
    [appendingAttString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                       value:(id)[NSNumber numberWithInt:style]
                       range:NSMakeRange(0, appendingAttString.length)];
    
    [self.attString appendAttributedString:appendingAttString];
    
    [self setNeedsDisplay];
}


- (void)addTitle:(NSString*)title withFont:(UIFont*)font withColor:(UIColor *)color
{
    [self addTitle:title withFont:font withColor:color withStyle:kCTUnderlineStyleNone];
}

- (void)addTitle:(NSString*)title withFont:(UIFont*)font
{
    [self addTitle:title withFont:font withColor:self.defaultColor];
}

- (void)addTitle:(NSString*)title withColor:(UIColor *)color
{
    [self addTitle:title withFont:self.defaultFont withColor:color];
}

+ (LLAttributedLabel*)attributeInfoLabel
{
    LLAttributedLabel *label = [[LLAttributedLabel alloc] initWithFrame:CGRectMake(16, 0, 300, 20)];
    label.backgroundColor = [UIColor clearColor];
    return label;
}
@end
